import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-record',
	templateUrl: './release-record.component.html',
})
export class ReleaseRecordComponent implements OnInit {
	tableData: any = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	startTime: string = ''; //开始时间
	endTime: string = ''; //结束时间
	selectedCompanyId: string = ''; //公司ID
	selectedCompanyName: string = ''; //公司名称
	deviceId: string = '';
	carNumber: string = '';
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
		this.load();
	}
	ngOnDestroy() {
		document.onclick = null;
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh() {
		this.load();
	}
	//初始化加载list数据
	load() {
		if (this.startTime == '' && this.endTime !== '') {
			this.jsonApiService.message('起始时间不能为空');
			return;
		}
		if (this.startTime !== '' && this.endTime == '') {
			this.jsonApiService.message('结束时间不能为空')
			return;
		}
		if (this.startTime !== '' && !common.checkDateFormat(this.startTime)) {
			this.jsonApiService.message('起始时间格式错误')
			return;
		}
		if (this.endTime !== '' && !common.checkDateFormat(this.endTime)) {
			this.jsonApiService.message('结束时间格式错误')
			return;
		}

		let reqdata = 'gridPager={"pageSize":' + this.pageSize + ',"nowPage":' + this.curPage + ',"parameters":{"searchKey":"' + this.searchKey +
			'","startTime":"' + this.startTime +
			'","endTime":"' + this.endTime +
			'","companyId":"' + this.selectedCompanyId +
			'","deviceId":"' + this.deviceId +
			'"}}'
		this.jsonApiService.http("carRelease/list.html", reqdata).subscribe(data => {
			if (data.isSuccess) {
				var result = data.exhibitDatas;
				this.tableData = result;
				this.totalCount = data.recordCount;
			}
		})
	}
	///分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除
	del_record(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("deviceInstall/delDeviceInstallById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.carNumber = data.carNumber;
		this.deviceId = data.deviceId;
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
}